﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;

namespace TestingServer
{
    class DataOperate
    {
        private static SqlConnection SQLConnection = new SqlConnection();
        public static String ErrorMessage;
        private static Boolean IsOpen = false;

        public static Boolean OpenData(String DataSource, String InitCatalog)
        {
            #region Building the connection string

            IsOpen = false;

            // Windows authentification - local connect
            //string ConnectionString = @"Initial Catalog=" + InitCatalog +
            //                          ";Data Source=" + DataSource +
            //                          ";Integrated Security=true" +
            //                          ";Connection Timeout=1;";
            string ConnectionString = @"Server = " + DataSource + "; Database = " + InitCatalog + "; User Id = sa; Password = joomlamaN;";
            #endregion

            #region Try to establish a connection to the database

            try
            {
                SQLConnection.ConnectionString = ConnectionString;
                SQLConnection.Open();
                //WriteToDbLog("Log", ConnectionString);

                IsOpen = true;
            }
            catch (Exception Ex)
            {
                // Try to close the connection
                if (SQLConnection != null)
                    SQLConnection.Dispose();

                // Create a (useful) error message
                ErrorMessage = "A error occurred while trying to connect to the server.";
                ErrorMessage += Environment.NewLine;
                ErrorMessage += Environment.NewLine;
                ErrorMessage += Ex.Message;
            }

            #endregion

            return IsOpen;
        }

        public static Exception BackupDatabase()
        {
            try
            {
                String fileName = String.Format("{0:yyMMddHHmmss}", DateTime.Now);

                SqlCommand cmd = new SqlCommand(String.Format("BACKUP DATABASE [Presbeton] TO  DISK = '{0}\\{1}.bak'", Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), fileName), SQLConnection);
                cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                return ex;
            }

            return null;
        }

        internal static void CloseData()
        {
            SQLConnection.Close();
            SQLConnection.Dispose();
        }

        public static SqlConnection DataConnection { get { return SQLConnection; } }

        public static void WriteValues(char identification, int stepNumber, string recipeName, int[] Values)
        {
            try
            {
                string insertMeasure =
@"Insert Into [dbo].[receive_data] 
(
    [identification],
    [step_number],
    [recipe_name],
    [recipe_cement],
    [recipe_struska],
    [recipe_kamenivo1],
    [recipe_kamenivo2],
    [recipe_kamenivo3],
    [recipe_kamenivo4],
    [recipe_kamenivo5],
    [recipe_prisady],
    [recipe_rezerva],
    [rezerva1],
    [rezerva2],
    [prumer_vlhkost3],
    [prumer_vlhkost4],
    [navyseni3],
    [navyseni4],
    [rezim_k3],
    [rezim_k4],
    [rezerva3],
    [rezerva4],
    [rezerva5],
    [rezerva6],
    [cement_silo1],
    [cement_silo2],
    [struska_silo3],
    [cement_silo4],
    [cement_silo5],
    [kamenivo_silo1],
    [kamenivo_silo2],
    [kamenivo_silo3],
    [kamenivo_silo4],
    [kamenivo_silo5],
    [prisady],
    [cement_struska_zbytek],
    [kamenivo_zbytek],
    [rezerva7],
    [rezerva8],
    [rezerva9],
    [rezerva10]
)
Values
(
	'{0}',
	{1},
	'{2}',
	{3},
	{4},
	{5},
	{6},
	{7},
	{8},
	{9},
	{10},
	{11},
	{12},
	{13},
	{14},
	{15},
	{16},
	{17},
	{18},
	{19},
	{20},
	{21},
	{22},
	{23},
	{24},
    {25},
    {26},
    {27},
    {28},
    {29},
    {30},
    {31},
    {32},
    {33},
    {34},
    {35},
    {36},
    {37},
    {38},
    {39},
    {40}
)";
                string SQLStatement = String.Format(insertMeasure,
                    identification,
                    stepNumber,
                    recipeName,
                    Values[0],
                    Values[1],
                    Values[2],
                    Values[3],
                    Values[4],
                    Values[5],
                    Values[6],
                    Values[7],
                    Values[8],
                    Values[9],
                    Values[10],
                    Values[11],
                    Values[12],
                    Values[13],
                    Values[14],
                    Values[15],
                    Values[16],
                    Values[17],
                    Values[18],
                    Values[19],
                    Values[20],
                    Values[21],
                    Values[22],
                    Values[23],
                    Values[24],
                    Values[25],
                    Values[26],
                    Values[27],
                    Values[28],
                    Values[29],
                    Values[30],
                    Values[31],
                    Values[32],
                    Values[33],
                    Values[34],
                    Values[35],
                    Values[36],
                    Values[37]);

                //WriteToDbLog("Msg", SQLStatement);

                try
                {
                    SqlCommand cmd = new SqlCommand(SQLStatement, SQLConnection);
                    cmd.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    string test = ex.Message;
                    //WriteToDbLog("Err", ex.Message);
                }
            }
            catch (Exception ex)
            {
                //WriteToDbLog("Err", ex.Message);
            }
        }
    }
}
